from urllib import request,parse
from fake_useragent import UserAgent
user_agent = UserAgent()

def get_url(data):
    url  = 'http://www.baidu.com/s?wd={}'
    res = parse.quote(data)
    return url.format(res)

def get_html(url):
    headers = {'User-Agent': user_agent.random}
    req = request.Request(url,headers=headers)
    res = request.urlopen(req)
    with open('work2.html', 'w', encoding='utf-8') as f:
        f.write(res.read().decode('utf-8'))

if __name__ == '__main__':
    data = input("请输入搜索内容：")
    url = get_url(data)
    get_html(url)
